<?php
namespace Admin\Controller\Link;
use Admin\Controller\Auto\AutoController;
use Admin\Controller\User\PersonalController;
	class LinkController extends AutoController {
		public function index(){
			$this->display();
		}
		public function insert(){
			$link=M('link');
			$data=I('post.');
			$data['site_link']='http://'.$data['site_link'];
			$upload = new PersonalController();
			$data['site_pic']=$upload->upload('./public/link');
			$data['time']=time();
			$data['uid']=session('id');
			$link->create($data);
			if($link->add()){
				echo '<script type="text/javascript">
					//输出父窗口的变量
					parent.link("yes");
				</script>';
			}else{
				echo '<script type="text/javascript">
					//输出父窗口的变量
					parent.link("no");
				</script>';
			}
		}
		public function update(){
			$data = I('post.');
			$data['site_link']='http://'.$data['site_link'];
			if($_FILES['pic']['name']){
				$pic = M('link')->where('id='.I('post.id'))->find()['site_pic'];
				$upload = new PersonalController();
				$data['site_pic']=$upload->upload('./public/link');
			}
			echo '<script type="text/javascript">
					//输出父窗口的变量
					parent.link_up('.json_encode($data).');
				</script>';
			$link = M('link');
			$link->create($data);
			if($link->save()){
				if($_FILES['pic']['name']){
					if(file_exists('.'.$pic)){
						unlink('.'.$pic);
					}
				}
				echo '<script type="text/javascript">
					//输出父窗口的变量
					parent.link_up("yes");
				</script>';
			}else{
				echo '<script type="text/javascript">
					//输出父窗口的变量
					parent.link_up("no");
				</script>';
			}
		}
		public function data(){
			// var_dump(session('id'));die;
			$data = M('link')->alias('a')
					->field(array('a.*,b.name'))
					->join('left join think_personal b on a.uid=b.uid')
					->select();
			foreach($data as &$v){
				$v['time']=date('Y-m-d',$v['time']);
			}
			$this->AjaxReturn($data);
		}
		public function del(){
			$pic = M('link')->where('id = '.I('post.id'))->find()['site_pic'];
			// var_dump($pic);
			if(M('link')->where('id='.I('post.id'))->delete()){
				if(file_exists('.'.$pic)){
					unlink('.'.$pic);
				}
				echo 'yes';
			}else{
				echo 'no';
			}
		}
	}